package StudentSystem;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void startStudentSystem(){
        ArrayList<Student> list = new ArrayList<>();
        loop: while (true) { //选中一部分代码按下ctrl + alt + T可以直接选择while进行包裹
            System.out.println("-----------------------------欢迎使用学生管理系统----------------------------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入您的选择：");
            Scanner sc = new Scanner(System.in);
            String choose = sc.next();
            switch(choose){
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    break loop;//上方将while循环命名为loop，这里将直接结束while循环。
                } //case后超出一行代码必须带{}  //停止运行也可用System.exit(0),表示停止虚拟机运行
                default -> System.out.println("没有这个选项！");
            }
        }
    }
    //添加学生
    public static void addStudent(ArrayList<Student> list){
        //1.利用空参构造先创建学生对象
        Student s = new Student();

        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生的id：");
            String id =sc.next();
            boolean flag = contains(list, id);
            if(flag){
                System.out.println("id已经存在，请重新输入！");
            }else{
                s.setId(id);
                break;
            }
        }

        System.out.println("请输入学生的姓名：");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生的年龄：");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生的家庭住址：");
        String address = sc.next();
        s.setAddress(address);
        //2.带参构造创建学生对象Student s = new Student(id,name,age,address);

        //把学生对象添加到集合当中
        list.add(s);

        //提示用户
        System.out.println("学生信息添加成功！");
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入想要删除学生的ID：");
            String id = sc.next();
            int index = getIndex(list,id);
            if(index >= 0){
                list.remove(index);
                System.out.println("删除成功。");
                break;
            }else{
                System.out.println("ID不存在，请重新输入");
            }
        }

    }
    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入要修改的学生的ID：");
            String id = sc.next();
            int index = getIndex(list, id);
            if(index == -1){
                System.out.println("学生ID不存在，请重新输入：");
            }else{
                Student stu = list.get(index);

                System.out.println("请输入新的ID：");
                String new_id = sc.next();
                stu.setId(new_id);

                System.out.println("请输入新的姓名：");
                String new_name = sc.next();
                stu.setName(new_name);

                System.out.println("请输入新的年龄：");
                int new_age = sc.nextInt();
                stu.setAge(new_age);

                System.out.println("请输入新的家庭住址：");
                String new_address = sc.next();
                stu.setAddress(new_address);
                break;
            }
        }

    }
    //查询
    public static void queryStudent(ArrayList<Student> list){
        if(list.size() == 0){
            System.out.println("当前暂无学生信息，请添加后再查询。");//如果系统中有学生信息就遍历集合，打印出所有学生信息
        }else{
            System.out.println("id\t\t姓名\t年龄\t家庭住址");
            for(int i = 0; i < list.size() ; i++){
                Student stu = list.get(i);
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getAddress());
            }//如果系统中有学生信息就遍历集合，打印出所有学生信息
        }
    }

    public static boolean contains(ArrayList<Student> list, String id){
        for(int i = 0; i < list.size(); i++){
            Student stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                return true;
            }
        }
        return false;
    }

    //通过id获取索引的方法
    public static int getIndex(ArrayList<Student> list, String id){
        for(int i = 0; i < list.size(); i++){
            Student stu = list.get(i);
            String sid = stu.getId();
            if(sid.equals(id)){
                return i;
            }
        }
        return -1;
    }

}
